package cn.com.jslh.boarpro.util.template;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.view.fragment.AddTemplateFragment;
import cn.com.jslh.boarpro.util.JSONUtil;

/**
 * Created by Administrator on 2017/11/17.
 */

public class TxtTempalte extends AddTempBaseItem implements AdapterInterface {
    @Override
    public String getFlag() {
        return "txt";
    }

    @Override
    public int getLayout() {
        return R.layout.add_template_select_item_layout;
    }

    @Override
    public SlimInjector<? extends AddTempBaseItem> getSlimInjetor() {
        SlimInjector<TxtTempalte> inj = new SlimInjector<TxtTempalte>() {
            @Override
            public void onInject(final TxtTempalte data, IViewInjector injector) {
                JSONObject config = new JSONObject();
                try {
                    config = new JSONObject(data.getConfig());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                final JSONObject finalConfig = config;
                injector
                        .text(R.id.titleText, data.getTitle())
                        .with(R.id.valueText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                TextView textView = (TextView) view;
                                String value;
                                if(data.getShowInfo()==null||data.getShowInfo().equals("")) {
                                    value = data.getPlaceholder();
                                }else {
                                    value = data.getShowInfo();
                                }
                                textView.setText(value);
                                JSONUtil.configValueTextView(textView, finalConfig);
                            }
                        })
                        .clicked(R.id.valueText, new View.OnClickListener() {
                            @Override
                            public void onClick(final View v) {
                                final EditText editText1 = new EditText(v.getContext());
                                String placeholder = data.getPlaceholder();
                                editText1.setHint(placeholder);
                                editText1.setText(data.getResult());
                                editText1.setGravity(Gravity.CENTER);
                                JSONUtil.configEditText(editText1, finalConfig);
                                new AlertDialog.Builder(v.getContext())
                                        .setTitle(data.getTitle())
                                        .setView(editText1)
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                if (!data.getResult().equals(editText1.getText().toString().trim())) {
                                                    data.setResult(editText1.getText().toString().trim());
                                                    data.setShowInfo(editText1.getText().toString().trim());
                                                    TemplateHelper.updataItems(v.getContext(), data);
                                                    AddTemplateFragment.notifyDatas();
                                                }
                                            }
                                        }).setNegativeButton("取消",null)
                                        .show();
                            }
                        })
                        .with(R.id.titleText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                JSONUtil.configTextView((TextView) view, finalConfig);
                            }
                        });
            }
        };
        return inj;
    }
}
